package com.yidu.t299.controller.twj;

import com.yidu.t299.contant.CommonConstant;
import com.yidu.t299.domain.twj.RoleTree;
import com.yidu.t299.domain.twj.SysRight;
import com.yidu.t299.domain.twj.UserInfo;
import com.yidu.t299.service.twj.SysRightBiz;
import com.yidu.t299.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author twj
 * @version 1.0.0
 */
@RestController
public class SysRightController {
    @Autowired
    private SysRightBiz sysRightBiz;

    /**
     * 新增
     */
    @PostMapping("sysRight/add")
    public Result add(@RequestBody SysRight sysRight) {
        sysRightBiz.insert(sysRight);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("sysRight/edit")
    public Result edit(@RequestBody SysRight sysRight) {
        sysRightBiz.update(sysRight);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("sysRight/remove/{rightCode}")
    public Result remove(@PathVariable String rightCode) {
        sysRightBiz.deleteByPrimaryKey(rightCode);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("sysRight/remove")
    public Result removeBatch(String[] array) {
        sysRightBiz.deleteBatch(array);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("sysRight/find/{rightCode}")
    public Result findById(@PathVariable String rightCode) {
        return Result.success(sysRightBiz.selectByPrimaryKey(rightCode));
    }

    /**
     * 分页查询
     */
    @GetMapping("sysRight/findPage")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                           SysRight sysRight) {
        return Result.success(sysRightBiz.findPage(pageNum, pageSize, sysRight));
    }

    /**
     * 批量查询
     */
    @GetMapping("sysRight/find")
    public Result find(SysRight sysRight) {
        return Result.success(sysRightBiz.findAll(sysRight));
    }

    /**
     * 查询当前登录用户的菜单
     */
    @GetMapping("sysRight/findMenu")
    public Result findMenu(HttpSession session) {
        //从session域中获取当前登录用户
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        List<SysRight> menus = null;
        if (userInfo != null) {
            menus = sysRightBiz.findMenu(userInfo.getUsrRoleId());
        }
        return Result.success(menus);
    }

    //////////////////////////////
    @PostMapping("sysRight/editRight")
    public List<RoleTree> editRight(){
        List<RoleTree> list = sysRightBiz.findAllRecursion();
        list.add(list.size(),new RoleTree("L01","育德教育",null));
        return list;
    }

    @GetMapping("sysRight/getAllRight/{roleId}")
    public List<String> getAllRight(@PathVariable("roleId") Integer roleId){
        List<String> list = sysRightBiz.getAllRight(roleId);
        return list;
    }

    @PostMapping("sysRight/updateRight/{nodeIds}/{roleId}")
    public void updateRight(@PathVariable("nodeIds") String[] nodeIds,@PathVariable("roleId") Integer roleId){
        if(roleId!=null){
            //先删除，再批量添加
            int a = sysRightBiz.deleteRoleRight(roleId);
            if(a>=0 && nodeIds !=null){
                sysRightBiz.insertBatchRoleRight(nodeIds, roleId);
            }
        }
    }
}
